<script setup lang="ts">
import { ref, watchEffect } from 'vue'
import { avatarList } from '@/common'
import type { AvatarDialogProps, AvatarDialogEmit } from './com.interface'

defineOptions({
  name: 'AvatarDialog_component',
})

// 向外部暴露打开和关闭方法
defineExpose({
  open: () => {
    visible.value = true
  },
  close: () => {
    visible.value = false
  },
})

const props = defineProps<AvatarDialogProps>()

const emit = defineEmits<AvatarDialogEmit>()

const visible = ref<boolean>(false)
const avatarSelected = ref<string>('')

watchEffect(() => {
  // console.log('watchEffect props', props)
  avatarSelected.value = props.avatar || '1'
})

// 切换头像
const changeUserAvatar = (avatar: string) => {
  avatarSelected.value = avatar
}

const beforeClose = () => {
  emit('closeProcessor')
  avatarSelected.value = props.avatar || '1'
}
</script>

<template>
  <ElDialog
    v-model="visible"
    title="选择头像"
    width="500"
    class="avatar-dialog-component"
    :before-close="beforeClose"
  >
    <div class="one-container">
      <div
        class="one-container-list"
        :class="[avatarSelected === item.id ? 'one-container-list-active' : '']"
        v-for="item in avatarList"
        :key="item.id"
        @click="changeUserAvatar(item.id)"
      >
        <img class="one-container-item" :src="item.avatar" alt="xfxj" />
      </div>
    </div>
    <template #footer>
      <div
        class="footer-container"
        @click.stop="emit('confirmProcessor', avatarSelected)"
      >
        确定
      </div>
    </template>
  </ElDialog>
</template>

<style scoped lang="scss">
.avatar-dialog-component {
  .one-container {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 8px;

    .one-container-list {
      width: 100px;
      height: 100px;
      border-radius: 5px;
      display: flex;
      justify-content: center;
      align-items: center;
      overflow: hidden;

      .one-container-item {
        width: 95px;
        height: 95px;
        border-radius: 5px;
        object-fit: cover;
        cursor: pointer;
        transition: transform 0.3s;

        &:hover {
          transform: scale(1.1);
        }
      }
    }

    .one-container-list-active {
      border: 3px solid var(--primary);
      // background-color: var(--primary);
    }
  }

  .footer-container {
    width: 100%;
    height: 30px;
    border-radius: 5px;
    text-align: center;
    line-height: 30px;
    outline: none;
    border-color: rgb(50.8, 116.6, 184.5);
    background-color: rgb(50.8, 116.6, 184.5);
    color: #fff;
    cursor: pointer;
    transition: all 0.3s;

    &:hover {
      background-color: #409eff;
      border-color: #409eff;
    }
  }
}
</style>
